﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static WinForm_Mes.ProductManagement.BaseSet;

namespace WinForm_Mes.ProductManagement
{
    /// <summary>
    /// 设备管理—设备类型
    /// </summary>
    public partial class EquipmentTypeForm : DevExpress.XtraEditors.XtraForm
    {
        //基础数据接口
        private string baseUrl = "http://localhost:5164";
        private HttpClient httpClient;
        public EquipmentTypeForm()
        {
            InitializeComponent();      
            this.Text = "设备类型";
            httpClient = new HttpClient();
            InitializeEvents();
        }
        // 绑定按钮事件
        private void InitializeEvents()
        {
            //查询按钮点击事件
            SearchButton.Click += SearchEquipmentType;
        }
        public void EquipmentType_Load(object sender, EventArgs e)
        {
            LoadEquipmentTypeData();
        }
        //加载设备类型数据
        public async void LoadEquipmentTypeData()
        {
            try
            {
                // 显示提示信息
                gridControl1.DataSource = null;
                // 调用后端API获取数据
                HttpResponseMessage response = await httpClient.GetAsync(baseUrl + "/api/EquipmentType/GetAllEquipmentType");
                if (response.IsSuccessStatusCode)
                {
                    string jsonContent = await response.Content.ReadAsStringAsync();
                    var apiResponse = JsonConvert.DeserializeObject<ApiResponse<EquipmentTypeModels>>(jsonContent);
                    var models = apiResponse.Items;
                    // 绑定数据到表格
                    gridControl1.DataSource = models;
                    // 设置列映射
                    gridView1.Columns["Id"].Visible = false;
                    gridView1.Columns["ParentId"].Visible = false;
                    gridView1.Columns["EquipmentTypeCode"].Caption = "设备类型编码";
                    gridView1.Columns["EquipmentTypeName"].Caption = "设备类型名称";
                    gridView1.Columns["IsEnabled"].Caption = "是否启用";
                    gridView1.Columns["Remark"].Caption = "备注";
                }
                else
                {
                    MessageBox.Show("获取规格数据失败！");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("发生错误：" + ex.Message);
            }
        }
        //查询设备管理—设备类型数据
        private async void SearchEquipmentType(object sender, EventArgs e)
        {
            try
            {
                var EquipmentTypeName = text_EquipmentTypeName.Text.Trim();
                var EquipmentStatusState = checked_EquipmentStatusState.Text.Trim();
                string queryParams = "?";
                if (!string.IsNullOrEmpty(EquipmentTypeName))
                {
                    queryParams += $"unitsCode={EquipmentTypeName}&";
                }
                if (!string.IsNullOrEmpty(EquipmentStatusState))
                {
                    queryParams += $"unitsName={EquipmentStatusState}&";
                }
                // 去掉最后一个 & 号
                if (queryParams.EndsWith("&"))
                {
                    queryParams = queryParams.Substring(0, queryParams.Length - 1);
                }
                // 如果只有问号则不附加参数
                if (queryParams == "?")
                {
                    queryParams = "";
                }
                HttpResponseMessage response = await httpClient.GetAsync(baseUrl + "/api/EquipmentType/GetAllEquipmentType" + queryParams);
                if (response.IsSuccessStatusCode)
                {
                    string jsonContent = await response.Content.ReadAsStringAsync();
                    var apiResponse = JsonConvert.DeserializeObject<ApiResponse<EquipmentTypeModels>>(jsonContent);
                    var Specifications = apiResponse.Items;
                    // 绑定数据到表格
                    gridControl1.DataSource = Specifications;
                }
                else
                {
                    MessageBox.Show("查询设备类型数据失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询发生错误：" + ex.Message);
            }
        }



        // 设备管理—设备类型数据模型
        public class EquipmentTypeModels
        {
            /// <summary>
            /// 主键Id
            /// </summary>
            public long Id { get; set; }
            /// <summary>
            /// 父级分类Id
            /// </summary>
            public long ParentId { get; set; }
            /// <summary>
            /// 设备类型编码
            /// </summary>
            public string EquipmentTypeCode { get; set; }
            /// <summary>
            /// 设备类型名称
            /// </summary>
            public string EquipmentTypeName { get; set; }
            /// <summary>
            /// 是否启用
            /// </summary>
            public bool IsEnabled { get; set; } = true;
            /// <summary>
            /// 备注
            /// </summary>
            public string Remark { get; set; }
        }
        // API响应包装类
        public class ApiResponse<T>
        {
            public int Total { get; set; } = 0;
            public int Index { get; set; } = 1;
            public int Size { get; set; } = 10;
            public List<T> Items { get; set; } = new List<T>();
        }
    }
}